package com.jk11a.shangdaast.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


import android.util.Log;

import com.jk11a.shangdaast.user.User;

public class UserDao {

	public static int USERID;
	List<NameValuePair> list = null;
	
	public int login(String name,String password){
		int flag2=0;   //标志用户id
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/login";
			//String urlString = "http://192.168.1.102:8080/ShangDaAstServer/login";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 JSONObject jsonObject = new JSONObject();  
			 nameValuePair.add(new BasicNameValuePair("name",name));
			 nameValuePair.add(new  BasicNameValuePair("password", password)); 
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));	
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 Integer id= (Integer) result.get("id");
			 USERID=id;
			 String flag=(String)result.get("flag");
			 if(flag.equals("success")){
				 flag2=id;
			 }
			 else{
				flag2=0;
			 }
	}catch(Exception e){
		e.printStackTrace();
	}
		return flag2;
	}
	public void modify(User user){
		
	}
	public User regist(User user){
		User user2=new User();
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/regist";
			
			HttpPost httpPost = new HttpPost(urlString); 
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 JSONObject jsonObject = new JSONObject();  
			 nameValuePair.add(new BasicNameValuePair("name",user.getName()));
			 nameValuePair.add(new  BasicNameValuePair("password", user.getPassword())); 
			 nameValuePair.add(new BasicNameValuePair("userid",user.getUserid()));
			 nameValuePair.add(new BasicNameValuePair("class",user.getClass_()));
			 nameValuePair.add(new BasicNameValuePair("college",user.getCollege()));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));	
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
			 Log.i("http post", httpPost.getHeaders("name").toString());
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 user2.setName(user.getName());
			 user2.setPassword(user.getPassword());
			 String flag=(String)result.get("flag");
			 Log.i("here", "flag="+flag);
			 user2.setFlag(flag);
			 
	}catch(Exception e){
		e.printStackTrace();
	}
		return user2;
		
	}
	
	public User getuser(int id){
		User user=new User();
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/getuser";
			HttpPost httpPost = new HttpPost(urlString); 
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 JSONObject jsonObject = new JSONObject();  
			 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));	
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 user.setName((String)result.get("name"));
			 user.setClass_((String)result.get("class_"));
			 user.setCollege(result.getString("college"));
			 user.setUserid(result.getString("userid"));
			 user.setPassword(result.getString("password"));
			 user.setSchoolcard(result.getString("schoolcard"));
			 user.setLibpwd(result.getString("libpwd"));
		}catch(Exception e){
			e.printStackTrace();
		}
		return user;
	}

}
